package com.bawei.mall.product.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.bawei.common.core.domain.R;
import com.bawei.common.core.es.EsIndex;
import com.bawei.mall.product.domain.Advertise;
import com.bawei.mall.product.domain.Po.Brand;
import com.bawei.mall.product.domain.Po.HomeFlashPromotion;
import com.bawei.mall.product.domain.Po.Product;
import com.bawei.mall.product.domain.Vo.BrandVo;
import com.bawei.mall.product.mapper.CProductMapper;
import com.bawei.mall.product.remote.RemoteAdvertiseInfo;
import com.bawei.mall.product.service.ICProductService;
import com.bawei.system.remote.api.RemoteEsDocService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @Author：liurui
 * @Package：com.bawei.mall.search.service.impl
 * @Project：mall_cloud
 * @name：UserProductServiceImpl
 */
@Service
public class CProductServiceImpl implements ICProductService {
    @Autowired
    private CProductMapper mapper;

    @Autowired
    private RemoteAdvertiseInfo remoteAdvertiseInfo;

    @Autowired
    private RemoteEsDocService remoteEsDocService;

    @Override
    public List<Product> recommendProductList() {
        return mapper.recommendProductList();
    }

    @Override
    public List<Product> productList(Integer brandId) {
        return mapper.productList(brandId);
    }

    @Override
    public List<Product> keyWordSearch(String keyWord) {
        return mapper.keyWordSearch(keyWord);
    }

    @Override
    public List<String> association(String keyWord) {
        R r = remoteEsDocService.querySuggestions(EsIndex.PRODUCT_INFO, keyWord, "titleSuggest");
        if(r.isSuccess()){
            if(r.getData() != null){
                return JSONObject.parseObject(JSONObject.toJSONString(r.getData()),List.class);
            }
        }else {
            throw new RuntimeException(r.getMsg());
        }
        return null;
    }

    @Override
    public BrandVo content() throws ExecutionException, InterruptedException {
        //查询广告轮播图
        CompletableFuture<List<Advertise>> advertiseFuture = CompletableFuture.supplyAsync(() -> mapper.findAdvertise());
        //查询品牌
        CompletableFuture<List<Brand>> brandFuture = CompletableFuture.supplyAsync(() -> {
            List<Brand> list = mapper.content();
            //TODO 测试数据，后续可迭代为MyBatis动态查询
            for (Brand brand : list) {
                Integer brandCount = mapper.findAll(brand.getId());
                brand.setProductCount(brandCount);
            }
            return list;
        });
        //查询秒杀
        CompletableFuture<List<Product>> flashProduct = CompletableFuture.supplyAsync(() -> mapper.findByFlashProductList());
        //查询推荐
        CompletableFuture<List<Product>> hotProduct = CompletableFuture.supplyAsync(() -> mapper.findByHotProductList());
        //查询新鲜好物
        CompletableFuture<List<Product>> newProduct = CompletableFuture.supplyAsync(() -> mapper.findByNewProductList());
        //秒杀
        CompletableFuture<HomeFlashPromotion> flashPromo = CompletableFuture.supplyAsync(() -> mapper.findByHomeFlashPromotion());

        // 等待所有异步操作完成
        List<Advertise> advertiseList = advertiseFuture.get();
        List<Brand> brandList = brandFuture.get();
        List<Product> flashProductList = flashProduct.get();
        List<Product> hotProductList = hotProduct.get();
        List<Product> newProductList = newProduct.get();
        HomeFlashPromotion flashPromotion = flashPromo.get();
        // 查询秒杀数据和新鲜好物等（这里假设为同步查询）


        // 构造HomeFlashPromotion对象（这里我们假设它不需要再次异步处理）
        HomeFlashPromotion homeFlashPromotion = new HomeFlashPromotion();
        homeFlashPromotion.setId(flashPromotion.getId());
        homeFlashPromotion.setStartTime(flashPromotion.getStartTime());
        homeFlashPromotion.setEndTime(flashPromotion.getEndTime());
        // ... 设置其他秒杀数据
        homeFlashPromotion.setProductList(flashProductList);

        // 构造BrandVo对象并返回
        BrandVo brandVo = new BrandVo();
        brandVo.setAdvertiseList(advertiseList);
        brandVo.setBrandList(brandList);
        brandVo.setHomeFlashPromotion(homeFlashPromotion);
        brandVo.setHotProductList(hotProductList);
        brandVo.setNewProductList(newProductList);

        return brandVo;
    }




}
